// index.js
import {
  get,
  post
} from '../../utils/request'
import {
  APIS
} from '../../utils/api'
import { timestampToTime } from '../../utils/util'
// 获取应用实例
const app = getApp()

Page({
  data: {
    shopInfo: {},
    vipList: [],
    pn: 1,
    input: '',
    userSearch: false
  },
  onLoad() {
    let shopInfo = wx.getStorageSync('currShop');
    this.setData({
      shopInfo
    }, () => {
      this.getVipList();
    });
  },
  onReachBottom() {
    if (this.data.userSearch) {
      this.getVipSearch(this.data.pn);
    }
    else {
      this.getVipList(this.data.pn)
    }
  },

  getVipList(pn = 1) {
    get(`${APIS.vip}/list`, {
      pn,
      shop: this.data.shopInfo.id
    })
      .then(res => {
        console.log(res.data)
        this.setData({
          vipList: res.data,
          pn: pn + 1,
          userSearch: false
        })
      })
  },
  getVipSearch(pn = 1) {
    get(`${APIS.vip}/search`, {
      search: this.data.input,
      pn,
      shop: this.data.shopInfo.id
    })
      .then(res => {
        console.log(res);
        const vipList = pn === 1 ? res.data || [] : this.data.vipList.concat(res.data || []);
        this.setData({
          vipList,
          pn: pn + 1,
          userSearch: true
        })
      })
  },
  handleShopChange(e) {
    const shopInfo = e.detail;
    this.setData({
      shopInfo
    });
    this.setData({
      input: ''
    }, () => {
      this.getVipList();
    })
  },
  handleInput(e) {
    let value = e.detail.value;
    this.setData({
      input: value
    })
    if (!value) {
      this.getVipList();
    }
  },
  handleSearch() {
    console.log(this.data.input)
    if (!this.data.input) {
      this.getVipList()
      return;
    }
    this.getVipSearch();
  },
  handleJump(e) {
    let userInfo = e.currentTarget.dataset.info;
    let {shopInfo} = this.data;
    wx.navigateTo({
      url: `/pages/vipDetail/index?id=${userInfo.id}&shop=${shopInfo.id}&shopname=${shopInfo.name}`
    })
  }
})